<?php
/**********************************************************************************
	
	Lists all matches to a given string or integer
	
	Copyright (C) 2010 Benjamin Thomas
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 	Author: Benjamin Thomas
	Email: thomas.ben@gmail.com
	Created: 07.15.10
 
	Inputs: 
		q	A string or integer to search in the item's database
	Calls: None.
 
 **********************************************************************************
 */

require_once(dirname(__FILE__) . '/../../lib/PageLayout.php');
require_once(dirname(__FILE__) . '/../../lib/MarketDB.php');
require_once(dirname(__FILE__) . '/../../lib/dateFunctions.php');

if (array_key_exists('q', $_GET)) {
	$q = $_GET['q'];
} else {
	printSearchBox();
	echo "You must enter something to search for.";
	exit();
}

$db = new MarketDB();

if (is_numeric($q)) {
	$where = "itemid = $q";
} else {
	//Safety first
	$safe_q = mysql_real_escape_string($q);
	$where = " name LIKE '%$safe_q%'";
}

$sql = "SELECT name, icon, itemid FROM items WHERE $where";
$results = $db->publicQuery($sql);
?>
<div id="pager" class="pager" style="text-align:right; magin-top: 10px; margin-bottom: -15px;"> 
	<form> 
		<img src="images/first.png" class="first"/> 
		<img src="images/prev.png" class="prev"/>
		<span style="color: black" class="pagedisplay"></span>
		<img src="images/next.png" class="next"/> 
		<img src="images/last.png" class="last"/> 
		<select class="pagesize"> 
			<option  value="10">10</option> 
			<option selected="selected"  value="20">20</option> 
			<option value="30">30</option> 
			<option  value="40">40</option> 
		</select> 
	</form> 
</div> 
<?php
echo '<table id="searchtable" class="tablesorter"><thead><tr><th>Item</th><th>All Auctions</th><th>Sales</th><th>Purchases</th><th>Recipe</th></tr></thead>';
echo "<tbody>";
foreach($results as &$row) {
	echo "<tr>";
	echo '<td><img src="images/icons/' . $row['icon'] . '" style="width:20px;height:20px;" /> ' . $row['name'] . '</td>';
	$sql = "SELECT sum(stackcount) as sum FROM completedAuctions WHERE itemid = " . $row['itemid'];
	$sales = $db->publicQuery($sql);
	$sql = "SELECT sum(stackcount) as sum FROM completedBuyouts WHERE itemid = " . $row['itemid'];
	$buys = $db->publicQuery($sql);
	$sum = 0;
	if ($sales[0]['sum']) { $sum += $sales[0]['sum']; $sales = $sales[0]['sum']; } else { $sales = 0; }
	if ($buys[0]['sum']) { $sum += $buys[0]['sum'];  $buys = $buys[0]['sum']; } else { $buys = 0; }
	echo '<td><a href="#" class="link" onClick="showAuc(14,' . $row['itemid']. ')">' . $sum . '</a></td>';
	echo '<td><a href="#' . $row['itemid'] . '" class="link" onClick="showSales(14,' . $row['itemid']. ')">' . $sales . '</a></td>';
	echo "<td><a href='#' onClick='showPurchases(14," . $row['itemid'] . ")' class='link'>" . $buys . "</a></td>";
	if ($craft = $db->isCraftable($row['itemid'])) {
		echo '<td><a href="#' . $craft['craftID'] . '" onClick="showRecipe(' . $craft['craftID'] . ')">Recipe</a></td>';
	} else {
		echo '<td>-</td>';
	}
	echo '</tr>';
}				
echo "</tbody></table>";	
 ?> 
<script>
	
$(document).ready(function() 
    {     
		$(function() { 
        $("#searchtable").tablesorter({ 
			sortList: [[0,1]],
            headers: { 
                0: { sorter: 'image' },
				1: { sorter: 'digit' },
				2: { sorter: 'digit' },
				3: { sorter: 'digit' },
				4: { sorter: false },
            },
			widthFixed: true,
			widgets: ['zebra']			
        }); 

		$("#searchtable").tablesorterPager({container: $("#pager")});
    });      

    } 
); 
    
</script>